﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using moose;

namespace mooseirc.controls
{
    /// <summary>
    /// Custom Input Control
    /// </summary>
    public class InputControl : TextBox
    {

        #region [Local Attributes]

        /// <summary>
        /// Collects everything thats been inputted
        /// </summary>
        private LinkedList<string> m_inputs;

        /// <summary>
        /// Maintains positino in selection
        /// </summary>
        private LinkedListNode<string> m_inputPosition;

        #endregion

        #region [Events]

        /// <summary>
        /// Event Indicates User Input
        /// </summary>
        public event ResponseHandler UserResponse;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Input Control
        /// </summary>
        public InputControl()
            : base()
        {
            m_inputs = new LinkedList<string>();
            UserResponse += onUserResponse;
            return;
        }

        #endregion

        #region [Overrides]

        /// <summary>
        /// Fires on key press
        /// </summary>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                if (Text.Length > 0)
                {
                    UserResponse(Text);
                    Clear();
                }
            }
            base.OnKeyPress(e);
        }

        /// <summary>
        /// Fires when key is held down
        /// </summary>
        protected override void OnKeyDown(KeyEventArgs e)
        {

            //TODO: this is SO close, almost, almost there

            if (m_inputPosition == null) return;
            if (e.KeyCode == Keys.Up)
            {
                if (m_inputPosition.Previous == null) return;
                m_inputPosition = m_inputPosition.Previous;
                Text = m_inputPosition.Value;
                e.Handled = true;
                Select(Text.Length, 1);
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (m_inputPosition.Next == null) return;
                m_inputPosition = m_inputPosition.Next;
                Text = m_inputPosition.Value;
                e.Handled = true;
                Select(Text.Length, 1);
            }
            base.OnKeyDown(e);
        }

        #endregion

        #region [Internal Event Handlers]

        /// <summary>
        /// Fires when user hits enter, with text in the box
        /// </summary>
        /// <param name="response"></param>
        private void onUserResponse(string response)
        {
            if (m_inputs.Last == null)
                m_inputs.AddLast(response);
            else if (m_inputs.Last.Value != response)
                m_inputs.AddLast(response);
            m_inputPosition = m_inputs.Last;
            return;
        }

        #endregion

    }
}
